/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2000-2001  Javier Kohen <jkohen at tough.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package ar.com.jkohen.awt;

import java.awt.*;

public class BorderedPanel extends Panel
{
    private String tag;
	
	public static int CENTER = 0;
	public static int RIGHT = 1;
	public static int LEFT = 3;
	
	public static int SUNKEN = 0;
	public static int RAISED = 1;	
	
	public static int justify = CENTER;
	public static int edge = SUNKEN;
	
    public BorderedPanel()
	{
    }

    public BorderedPanel(String tag)
	{
		setTag(tag);
    }
	
	public void setTag(String t)
	{
		if (t != null)
			this.tag = " " + t + " ";
		else
			this.tag = "";
		repaint();
	}
	
	public void update(Graphics g)
    {
		paint(g);
    }
	
	public void paint(Graphics g)
	{
        super.paint(g);
		FontMetrics fm = getFontMetrics(getFont());
		Color c = getBackground();
        int x = 0;
        int y = fm.getHeight() / 2;
        int a = getSize().width;
        int b = getSize().height;
        g.clearRect(0, 0, a, b);
		
		if (edge == SUNKEN)
			g.setColor(c.darker());
		else
	        g.setColor(c.brighter());
        g.drawLine(x, y, x, b - 2);
        g.drawLine(x, y, a - 2, y);
        g.drawLine(x, b - 2, a - 2, b - 2);
        g.drawLine(a - 2, y + 1, a - 2, b - 1);
		
        if (edge == SUNKEN)
			g.setColor(c.brighter());
		else
	        g.setColor(c.darker());
        g.drawLine(x + 1, y + 1, x + 1, b - 3);
        g.drawLine(x + 1, y + 1, a - 4, y + 1);
        g.drawLine(x, b - 1, a - 1, b - 1);
        g.drawLine(a - 1, y, a - 1, b - 1);
		
        if (tag != null && tag.length() > 0)
        {
			int l = fm.stringWidth(tag);
			
			if (justify == CENTER)
				x = (a - l) / 2;
			if (justify == RIGHT)
				x = getInsets().left * 2;
			if (justify == LEFT)
				x = a - l - getInsets().right * 2;
			
			g.clearRect(x, 0, l, fm.getHeight());
			g.setColor(super.getForeground());
			g.drawString(tag, x, fm.getAscent());
		}
	}
	
	public Insets getInsets()
	{
		FontMetrics fm = getFontMetrics(getFont());
		return new Insets(fm.getHeight(), 6, 6, 6);
	}
}
